package com.adds.lvds.controller.code;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.model.code.SysCodeType;
import com.adds.lvds.security.BaseController;
import com.adds.lvds.service.impl.code.SysCodeService;

@Controller
@RequestMapping(value = "/codeType/")
public class SysCodeTypeController extends BaseController<SysCodeType> {
	@Inject
	private SysCodeService sysCodeService;

	/**
	 * 跳转到代码设置页
	 * 
	 * @return
	 */
	@RequestMapping("index")
	public String index(Model model, HttpServletRequest request) {
		String htmlBut = (String) request.getAttribute("htmlBut");
		model.addAttribute("htmlBut", htmlBut);
		return "/code/listSysCodeType";
	}

	/**
	 * 加载代码设置数据
	 * 
	 * @param request
	 * @param response
	 * @param sysCodeType
	 *            获取查询条件
	 * @param page
	 *            获取当前页数,page是jqGrid自带参数
	 * @param rows
	 *            获取每页显示行数,rows是jqGrid自带参数
	 * @param sidx
	 *            获取排序列名,sidx是jqGrid自带参数
	 * @param sord
	 *            获取排序方式,sord是jqGrid自带参数
	 */
	@ResponseBody
	@RequestMapping(value = "loadDataGrid")
	public Map<String, Object> loadDataGrid(HttpServletRequest request,
			HttpServletResponse response,
			@Param(value = "sysCodeType") SysCodeType sysCodeType,
			@Param(value = "page") String page,
			@Param(value = "rows") String rows,
			@Param(value = "sidx") String sidx,
			@Param(value = "sord") String sord,
			@Param(value = "typeName") String typeName) {
		Map<String, Object> map = new HashMap<String, Object>();
		pageView = sysCodeService.querySysCodeType(
				getPageView(page, rows, sidx, sord), sysCodeType);
		map.put("rows", pageView.getRecords());
		map.put("page", pageView.getPageNow());
		map.put("total", pageView.getPageCount());
		return map;
	}
	/**
	 * 添加代码类型数据
	 * 
	 * @param codeType
	 * @return
	 */
	@RequestMapping("addCodeType")
	@ResponseBody
	public Map<String, Object> addCodeType(
			@Param(value = "codeType") SysCodeType codeType) {

		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = sysCodeService.insertCodeType(codeType);
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "录入代码类型数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "录入代码类型数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 加载代码类型编辑页
	 * 
	 * @param model
	 * @param typeName
	 * @return
	 */
	@RequestMapping("editCodeType")
	public String editCodeType(Model model,
			@Param(value = "typeName") String typeName) {
		SysCodeType sysCodeType = sysCodeService.getCodeTypeByMap(typeName);
		model.addAttribute("sysCodeType", sysCodeType);
		return "/code/editSysCodeType";
	}
	/**
	 * 执行代码类型编辑操作
	 * 
	 * @param model
	 * @param codeType
	 * @return
	 */
	@ResponseBody
	@RequestMapping("doEditCodeType")
	public Map<String, Object> doEditCodeType(Model model,
			@Param(value = "codeType") SysCodeType codeType) {

		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = sysCodeService.updateCodeType(codeType);
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "编辑代码类型数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "编辑代码类型数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	/**
	 * 删除代码类型方法
	 * 
	 * @param model
	 * @param typeNames
	 * @return
	 */
	@ResponseBody
	@RequestMapping("deleteByTypeName")
	public Map<String, Object> deleteByTypeName(Model model, String typeNames) {
		Map<String, Object> map = new HashMap<String, Object>();
		String errmsg = "";
		String errcode = CommUtils.SUCC;
		String[] typeName = typeNames.split(",");
		for (int i = 0; i < typeName.length; i++) {
			if (!StringUtils.isEmpty(typeName[i])) {
				sysCodeService.deleteCodeType(typeName[i]);
			}
		}
		switch (errcode) {
		case CommUtils.SUCC:
			errmsg = "删除代码类型数据成功！";
			break;
		case CommUtils.ERROR:
			errmsg = "删除代码类型数据失败！";
			break;
		}
		map.put("errcode", errcode);
		map.put("errmsg", errmsg);
		return map;
	}

	

}
